import java.util.Arrays;

public class Test {
//    元素查找与位置返回‌
//    要求：用户输入一个整数，判断该数是否在数组中存在，若存在返回所有出现位置
    public static int[] search(int[] array,int key){
        int[] index = new int[10];
        int j = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key){
                index[j] = i;
                j++;
            }
        }
        return index;
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,4};
        int[] ret = search(array,4);
        System.out.println(Arrays.toString(ret));
    }
//    合并两个有序数组‌
//    要求：将两个升序排列的数组合并为一个新升序数组
//    示例输入：arr1 = {2, 5, 8}, arr2 = {3, 6, 9}
//    输出示例：[2, 3, 5, 6, 8, 9]
    public void merge2(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i != n; ++i) {
            nums1[m + i] = nums2[i];
        }
        Arrays.sort(nums1);
    }
    public static void merge(int[] array1,int[] array2){
        int[] newArray = new int[array1.length + array2.length];
        System.arraycopy(array1,0,newArray,0,array1.length);
        System.arraycopy(array2,0,newArray,array1.length,array2.length);
        Arrays.sort(newArray);
//        for (int i = 0; i < newArray.length - 1; i++) {
//            for (int j = 0; j < newArray.length - i - 1; j++) {
//                if(newArray[j] > newArray[j+1]){
//                    int temp = newArray[j];
//                    newArray[j] = newArray[j+1];
//                    newArray[j+1] = temp;
//                }
//            }
//        }
        System.out.println(Arrays.toString(newArray));
    }

    public static void main3(String[] args) {
        int[] arr1 = {2, 5, 8};
        int[] arr2 = {3, 6, 9};
        merge(arr1,arr2);
    }
//    统计奇偶数个数‌
//    要求：输入一个数组，统计其中奇数和偶数的数量
    public static void odd_eveCount(int[] array){
        int oddCount = 0;
        int eveCount = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] % 2 != 0){
                oddCount++;
            }else{
                eveCount++;
            }
        }
        System.out.println("奇数的数量为:"+oddCount);
        System.out.println("偶数的数量为:"+eveCount);
    }

    public static void main2(String[] args) {
        int[] array = {6, 7, 9, 12, 4};
        odd_eveCount(array);
    }

//    将数组元素逆序存放至新数组并输出
    public static int[] reverse(int[] array){
        int[] array2 = new int[array.length];
        int j = array.length;
        for (int i = 0; i < array.length; i++) {
            array2[i] = array[j - 1];
            j--;
        }
        return array2;
    }

    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println(Arrays.toString(reverse(array)));
    }
}

